﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Inference
{
	public partial class frmTemplateEditor : Form
	{
		public Frame Situation
		{
			get;
			set;
		}
		TaleEntities tale;
		public frmTemplateEditor(Frame Situation)
		{
			InitializeComponent();
			this.Situation = Situation;
			this.Text = "Шаблон для ситуации '" + Situation.Name + "'";
			tale = new TaleEntities();

			ucSlotPicker1.InitDomainPicker(2, 0);

			spcMain.Panel1.Enabled = true;
			spcMain.Panel2.Enabled = false;
		}

		public void switchMode()
		{
			spcMain.Panel1.Enabled = !spcMain.Panel1.Enabled;
			spcMain.Panel2.Enabled = !spcMain.Panel1.Enabled;
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			ucSlotPicker1.Reset();
			dgvTemplate.ClearSelection();
			switchMode();
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			ucSlotPicker1.InitFromPathStringCut(dgvTemplate.SelectedCells[0].Value.ToString());
			switchMode();
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("Вы действительны хотите удалить выбранные подстановки из шаблона?", "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
			{
				for (int i = dgvTemplate.SelectedRows.Count - 1; i > -1; i--)
				{
					dgvTemplate.Rows.RemoveAt(dgvTemplate.SelectedRows[i].Index);
				}
			}
		}

		private void dgvTemplate_SelectionChanged(object sender, EventArgs e)
		{
			btnDelete.Enabled = true;
			btnEdit.Enabled = false;
			if (dgvTemplate.SelectedCells.Count == 0)
			{
				ucSlotPicker1.Reset();
				btnDelete.Enabled = false;
			}
			else
				if (dgvTemplate.SelectedCells.Count == 1)
				{
					ucSlotPicker1.InitFromPathStringCut(dgvTemplate.SelectedCells[0].Value.ToString());
					//slotPicker.InitFromPathString(dgvTemplate.SelectedCells[0].Value.ToString());
					btnEdit.Enabled = true;
				}
				else
				{
				}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (dgvTemplate.SelectedCells.Count == 1)
			{
				dgvTemplate.SelectedCells[0].Value = ucSlotPicker1.PathStringCut;
				string ids = "";
				foreach (var item in ucSlotPicker1.GetSlotsPath())
				{
					ids += item.ID.ToString() + ",";
				}
				ids = ids.Substring(0, ids.Length - 1);
				dgvTemplate.SelectedCells[0].Tag = ids;
			}
			else
			{
				dgvTemplate.Rows.Add(ucSlotPicker1.PathStringCut);
				string ids = "";
				foreach (var item in ucSlotPicker1.GetSlotsPath())
				{
					ids += item.ID.ToString() + ",";
				}
				ids = ids.Substring(0, ids.Length - 1);
				dgvTemplate.Rows[dgvTemplate.Rows.Count - 1].Cells[0].Tag = ids;

			}

			switchMode();
		}

		private void btnCancelChanges_Click(object sender, EventArgs e)
		{
			if (dgvTemplate.SelectedCells.Count == 0)
			{
				if (dgvTemplate.Rows.Count > 0)
				{
					dgvTemplate.Rows[0].Selected = true;
				}
			}
			switchMode();
		}

		private void dgvTemplate_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			//MessageBox.Show((dgvTemplate.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag).ToString());
		}

		private void ucSlotPicker1_BadPicked()
		{
			btnSave.Enabled = false;
		}

		private void ucSlotPicker1_Picked()
		{
			btnSave.Enabled = true;
		}

	}
}
